import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'navigation_example.dart';
import 'start_page.dart';
import 'route/new_route.dart';
import 'route/echo_route.dart';

void main() {
  return runApp(
      new MaterialApp(
        initialRoute:"/splash",
        routes: {
          "/new_page":(context) => NewRoute(text:""),
          "/echo_page":(context) => EchoRoute(),
          "/":(context) => Home(),
          "/splash":(context) => const SplashPageLoading(),
        },
        onGenerateRoute: (settings){
          if(settings.name==""){
            return MaterialPageRoute(
              builder: (ctx){
                return NewRoute(text:settings.arguments.toString());
              }
            );
          }
          return null;
        },
      )
  );
}

class HomeRoute extends StatefulWidget{
  HomeRoute({Key? key, required this.title}) : super(key: key);
  final String title;
  @override
  State<StatefulWidget> createState() {
    return _HomeRouteState();
  }
}

class _HomeRouteState extends State<HomeRoute> {

  FToast fToast =  FToast();

  @override
  void initState() {
    super.initState();
    fToast.init(context);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Home route"),
      ),
      body: Center(
        child: TextButton(
          child: Text("open new route"),
          onPressed: () async {
            //导航到新路由
            // var result = await Navigator.push(
            //   context,
            //   MaterialPageRoute(
            //       builder: (context) {
            //         return NewRoute(
            //           text: "HomeRoute 传过来的值",
            //         );
            //       },
            //       maintainState: false
            //   ),
            // );
            // Navigator.pushNamed(context, "new_page");
            var result = await Navigator.pushNamed(context, "echo_page", arguments: "HomeRoute 传过来的值");
            fToast.showToast(
              child: Text(result.toString(),style: TextStyle()),
              gravity: ToastGravity.CENTER,
              toastDuration: Duration(seconds: 2),
            );
          },
        ),
      ),
    );
  }

}